package com.ruoyi.local.controller;

import java.util.List;

import com.ruoyi.common.core.page.TableDataInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.local.domain.SysAppmenu;
import com.ruoyi.local.service.ISysAppmenuService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * APP菜单权限Controller
 * 
 * @author ruoyi
 * @date 2020-12-18
 */
@Controller
@RequestMapping("/local/appmenu")
public class SysAppmenuController extends BaseController
{
    private String prefix = "local/appmenu";

    @Autowired
    private ISysAppmenuService sysAppmenuService;



    @RequiresPermissions("local:appmenu:view")
    @GetMapping()
    public String appmenu()
    {
        return prefix + "/appmenu";
    }


    /**
     * 查询APP菜单权限树列表
     */
    @RequiresPermissions("local:appmenu:list")
    @PostMapping("/list")
    @ResponseBody
    public List<SysAppmenu> list(SysAppmenu sysAppmenu)
    {
        List<SysAppmenu> list = sysAppmenuService.selectSysAppmenuList(sysAppmenu);
        return list;
    }

    /**
     * 导出APP菜单权限列表
     */
    @RequiresPermissions("local:appmenu:export")
    @Log(title = "APP菜单权限", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysAppmenu sysAppmenu)
    {
        List<SysAppmenu> list = sysAppmenuService.selectSysAppmenuList(sysAppmenu);
        ExcelUtil<SysAppmenu> util = new ExcelUtil<SysAppmenu>(SysAppmenu.class);
        return util.exportExcel(list, "appmenu");
    }

    /**
     * 新增APP菜单权限
     */
    @GetMapping(value = { "/add/{menuId}", "/add/" })
    public String add(@PathVariable(value = "menuId", required = false) Long menuId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(menuId))
        {
            mmap.put("sysAppmenu", sysAppmenuService.selectSysAppmenuById(menuId));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存APP菜单权限
     */
    @RequiresPermissions("local:appmenu:add")
    @Log(title = "APP菜单权限", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysAppmenu sysAppmenu)
    {
        return toAjax(sysAppmenuService.insertSysAppmenu(sysAppmenu));
    }

    /**
     * 修改APP菜单权限
     */
    @GetMapping("/edit/{menuId}")
    public String edit(@PathVariable("menuId") Long menuId, ModelMap mmap)
    {
        SysAppmenu sysAppmenu = sysAppmenuService.selectSysAppmenuById(menuId);
        mmap.put("sysAppmenu", sysAppmenu);
        return prefix + "/edit";
    }

    /**
     * 修改保存APP菜单权限
     */
    @RequiresPermissions("local:appmenu:edit")
    @Log(title = "APP菜单权限", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysAppmenu sysAppmenu)
    {
        return toAjax(sysAppmenuService.updateSysAppmenu(sysAppmenu));
    }

    /**
     * 删除
     */
    @RequiresPermissions("local:appmenu:remove")
    @Log(title = "APP菜单权限", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{menuId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("menuId") Long menuId)
    {
        return toAjax(sysAppmenuService.deleteSysAppmenuById(menuId));
    }

    /**
     * 选择APP菜单权限树
     */
    @GetMapping(value = { "/selectAppmenuTree/{menuId}", "/selectAppmenuTree/" })
    public String selectAppmenuTree(@PathVariable(value = "menuId", required = false) Long menuId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(menuId))
        {
            mmap.put("sysAppmenu", sysAppmenuService.selectSysAppmenuById(menuId));
        }
        return prefix + "/tree";
    }

    /**
     * 加载APP菜单权限树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = sysAppmenuService.selectSysAppmenuTree();
        return ztrees;
    }
}
